﻿$.extend({

    UserManagementGroups: {

        LoadPage: function () {

            var jqxhr = $.get('html/ManageForm.html');
            jqxhr.success(function (data) {

                $('#ManageFormDialog').html(data);
                $.UserManagementGroups.SetOnClickEvents();
                $.UserManagement.SetButtonStyle();
                $.UserManagement.SetGlobalClickEvents();

                // Wire up jQueryUI styling to buttons
                //                $("#btnCloseGroupDialog").button();
                //                $("#btnAddUsers").button();
                //                $("#btnRemoveUsers").button();

            });

            jqxhr.error(function () {
                alert('Failed to load edit form.');
            });
        },

        SetOnClickEvents: function () {

            // on Click event for adding/removing groups - Group Management
            $('#btnAddItems').click(function () {
                $("#ManageFormTable").hide();
                $("#LoadingImage").show();
                $.UserManagementGroups.AddUserToGroups($('#lblEntityName').html());
                $("#LoadingImage").hide();
                $("#ManageFormTable").show();
            });

            $('#btnRemoveItems').click(function () {
                $("#ManageFormTable").hide();
                $("#LoadingImage").show();
                $.UserManagementGroups.RemoveUserFromGroups($('#lblEntityName').html());
                $("#LoadingImage").hide();
                $("#ManageFormTable").show();
            });
        },

        PopulateDataTable: function () {

            try {
                $.UserManagementGroups.LoadPage();

                // Initialize Group(s) Data Table
                var arryGroups = $.UserManagementDA.GetAllGroups();
                var dt = $('#groupListTable').DataTable();
                dt.clear().rows.add(arryGroups).draw();

                // Wire up the data table to handle row selection
                $("#groupListTable tbody").delegate("tr", "click", function () {
                    var groupName = $("td:first", this).text();
                    var groupDescription = $("td:eq(1)", this).text();
                    $.UserManagementGroups.PopulateFormPage(groupName, groupDescription);
                    //$.UserManagementGroups.PopulateEditFormForGroupDisplay(groupName, groupDescription);
                });

                $("#groupListTable td:first-child").addClass("nowrap");
            }
            catch (e) {
                alert(e);
            }
        },

        PopulateFormPage: function (groupName, description) {

            try {
                //var arryUserInfo = $.UserManagementDA.GetUserInfo(loginName);
                $.UserManagementGroups.RefereshUserLists(groupName);
                var groupUrl = $().SPServices.SPGetCurrentSite() + "/_layouts/people.aspx?MembershipGroupId=" + $.UserManagementGroups.GetGroupID(groupName);

                $('#EntityTypeTitle').text("Group Name:");
                $('#lblInstruction').text(instructions[0].Text);
                $('#AvailableListBoxTitle').text("Available Users");
                $('#AssignedistBoxTitle').text("Selected Users");

                $("#lblEntityName").text(groupName);
                $("#lblGroupDescription").text(description);
                $("#lblEntityUrlName").text("View Group Info");
                $("#lblEntityUrlName").attr("href", groupUrl);
                $('#GroupDescription').show();
                $('#ManageFormDialog').dialog('open');
            }
            catch (e) {
                alert(e);
            }
        },

        RefereshUserLists: function (groupName) {

            $.UserManagement.ClearListBoxItems();
            $('#filterAvailableItems').val('Search for Group(s)...');
            $('#filterSelectedItems').val('Search for Group(s)...');

            var arryAllUsers = $.UserManagementDA.GetAllUsers();
            var arrySelectedUsers = $.UserManagementDA.GetAllUsersByGroup(groupName);
            var arryFilteredUsers = $.UserManagement.FilterAvailableUsers(arryAllUsers, arrySelectedUsers);

            $.UserManagementGroups.PopulateUserLists("#lstBxAvailableItems", arryFilteredUsers);
            $.UserManagementGroups.PopulateUserLists("#lstBxSelectedItems", arrySelectedUsers);

            $('#lstBxAvailableItems').filterByText($('#filterAvailableItems'), false);
            $('#lstBxSelectedItems').filterByText($('#filterSelectedItems'), false);

            $.UMUtilities.SetListBoxHeight("#lstBxSelectedItems");
            $.UMUtilities.SetListBoxHeight("#lstBxAvailableItems");
        },

        PopulateUserLists: function (listBoxID, arryUsers) {

            var arr = [];
            var str;
            $.each(arryUsers, function (val, text) {
                str = text.LoginName;
                if (str.indexOf("\\") >= 0) {
                    arr = str.split('\\');
                    str = arr[1];
                }
                $(listBoxID).append($('<option></option>').val(text.LoginName).text(text.UserName + " (" + str + ")"))
            });
        },

        GetGroupID: function (groupName) {

            var groupInfo = [];
            groupInfo = $.UserManagementDA.GetGroupInfo(groupName);
            return groupInfo[0].GroupID;
        },

        AddUserToGroups: function (groupName) {

            groupName = $.UMUtilities.DecodeText(groupName);

            if (groupName == null) {
                alert("No group selected.");
                return;
            }

            if ($("#lstBxAvailableItems").val() == null) {
                alert("You haven't selected any users to add.");
                return;
            }
            else {
                $('#lstBxAvailableItems option:selected').each(function () {
                    $.UserManagementDA.AddUserToGroup(groupName, $(this).val());
                    $('#lstBxSelectedItems').append($('<option></option>').val($(this).val()).text($(this).text()));
                });

                $("#lstBxAvailableItems option:selected").remove();
                $.UMUtilities.SetListBoxHeight("#lstBxSelectedItems");
                $.UMUtilities.SetListBoxHeight("#lstBxAvailableItems");
            }
        },

        RemoveUserFromGroups: function (groupName) {

            groupName = $.UMUtilities.DecodeText(groupName);

            if (groupName == null) {
                alert("No group selected.");
                return;
            }

            if ($("#lstBxSelectedItems").val() == null) {
                alert("You haven't selected any users to remove.");
                return;
            }
            else {
                $('#lstBxSelectedItems option:selected').each(function () {
                    $.UserManagementDA.RemoveUserFromGroup(groupName, $(this).val());
                    $('#lstBxAvailableItems').append($('<option></option>').val($(this).val()).text($(this).text()));
                });

                $("#lstBxSelectedItems option:selected").remove();
                $.UMUtilities.SetListBoxHeight("#lstBxSelectedItems");
                $.UMUtilities.SetListBoxHeight("#lstBxAvailableItems");
            }
        }
    }
});